package com.softme.trade.view.components;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.softme.trade.model.annotations.DataTable;
import com.softme.trade.util.ReflectUtil;
import com.softme.trade.util.StringUtil;

public class DataTableUtils
{
	public static List<ColumnModel> createDynamicColumns(Class<?> objectType)
	{
		Method[] methods = objectType.getMethods();
		
		List<ColumnModel> columns = new ArrayList<ColumnModel>();
		
		for (int i = 0; i < methods.length; i++)
		{
			DataTable annotation = methods[i].getAnnotation(DataTable.class);
			
			if (annotation != null)
			{
				if (annotation.column())
				{
					String name = ReflectUtil.getMethodNameWithoutGetAndSet(methods[i]);
					String attribute = StringUtil.toFirstCharacterLowercase(name);
					
					if ( ! annotation.columnName().equals(""))
						columns.add(new ColumnModel(annotation.columnName(), attribute));
					else
						columns.add(new ColumnModel(name, attribute));
				}
			}
		}
		
		return columns;
	}
	
}
